<template>
  <div class="main-content" v-loading=loading element-loading-text="正在进入，清稍后....">
    <el-container style="height: 100%" v-if="!loading">
      <el-header class="main-content-header">
        <el-image class="logo" src="https://element-plus.org/images/element-plus-logo.svg"></el-image>
        <div style="flex: 1;padding: 20px;text-align: center;">
          <el-input v-model="searchInput.text" style="width: 100%;max-width: 500px;" placeholder="搜索论坛相关内容...">
            <template #prefix>
              <el-icon>
                <Search/>
              </el-icon>
            </template>
            <template #append>
              <el-select style="width: 120px" v-model="searchInput.type">
                <el-option value="1" label="帖子广场"></el-option>
                <el-option value="2" label="校园活动"></el-option>
                <el-option value="3" label="表白墙"></el-option>
                <el-option value="4" label="教务通知"></el-option>
              </el-select>
            </template>
          </el-input>

        </div>
        <div class="user-info">
          <el-popover placement="bottom" :width="350" trigger="click" >
            <template #reference>
              <el-badge style="margin-right: 15px" is-dot :hidden="!notification.length">
                <div class="notification">
                  <el-icon><Bell></Bell></el-icon>
                  <div style="font-size: 14px;">消息</div>
                </div>
              </el-badge>
            </template>
            <el-empty :image-size="80" description="暂时没有未读消息哦~" v-if="!notification.length"/>
            <el-scrollbar :max-height="500" v-else>
            <light-card v-for="item in notification" class="notification-item" @click="commitNotification(item.id, item.url)">
              <div>
                <el-tag size="small" :type="item.type">消息</el-tag>&nbsp;
                <span style="font-weight: bold">{{item.title}}</span>
              </div>
              <el-divider style="margin: 7px 0 3px 0"/>
              <div style="font-size: 13px; color: grey">
                {{item.content}}
              </div>
            </light-card>
            </el-scrollbar>
            <div style="margin-top:10px">
              <el-button size="small" type="info" icon="Check" @click="deleteAllNotification" style="width:100%" plain>清除全部未读消息</el-button>
            </div>
          </el-popover>
          <div class="profile">
            <div>{{ user.username }}</div>
            <div>{{ user.email }}</div>
          </div>
          <el-dropdown>
            <el-avatar :src="store.getters.avatarGet"></el-avatar>
            <template #dropdown>
              <el-dropdown-item>
                <el-icon>
                  <Operation/>
                </el-icon>
                个人设置
              </el-dropdown-item>
              <el-dropdown-item>
                <el-icon>
                  <Message/>
                </el-icon>
                消息列表
              </el-dropdown-item>

              <el-dropdown-item divided @click="userLogout">
                <el-icon>
                  <Back/>
                </el-icon>
                退出登录
              </el-dropdown-item>
            </template>
          </el-dropdown>
        </div>
      </el-header>
      <el-container>
        <el-aside width="230px">
          <el-scrollbar style="height: calc(100vh - 55px)">
            <el-menu default-openeds="[1,2,3]" style="height: 100vh" router :default-active="$route.path">
              <el-sub-menu index="1">
                <template #title>
                  <el-icon>
                    <Location/>
                  </el-icon>
                  <span>校园论坛</span>
                </template>
                <el-menu-item index="/index">
                  <template #title>
                    <el-icon>
                      <ChatDotSquare/>
                    </el-icon>
                    帖子广场
                  </template>
                </el-menu-item>
                <el-menu-item>
                  <template #title>
                    <el-icon>
                      <Bell/>
                    </el-icon>
                    失物招领
                  </template>
                </el-menu-item>
                <el-menu-item>
                  <template #title>
                    <el-icon>
                      <Notification/>
                    </el-icon>
                    校园活动
                  </template>
                </el-menu-item>
                <el-menu-item>
                  <template #title>
                    <el-icon>
                      <Umbrella/>
                    </el-icon>
                    表白墙
                  </template>
                </el-menu-item>
                <el-menu-item>
                  <template #title>
                    <el-icon>
                      <School/>
                    </el-icon>
                    海文考研
                    <el-tag style="margin-left: 10px" size="small">合作机构</el-tag>
                  </template>
                </el-menu-item>
              </el-sub-menu>
              <el-sub-menu index="2">

                <template #title>
                  <el-icon>
                    <Position/>
                  </el-icon>
                  <span>探索与发现</span>
                </template>
                <el-menu-item>
                  <template #title>
                    <el-icon>
                      <Bell/>
                    </el-icon>
                    校园动态
                  </template>
                </el-menu-item>
                <el-menu-item>
                  <template #title>
                    <el-icon>
                      <Files/>
                    </el-icon>
                    班级课程表
                  </template>
                </el-menu-item>
                <el-menu-item>
                  <template #title>
                    <el-icon>
                      <Document/>
                    </el-icon>
                    成绩查询
                  </template>
                </el-menu-item>
                <el-menu-item>
                  <template #title>
                    <el-icon>
                      <Monitor/>
                    </el-icon>
                    教务通知
                  </template>
                </el-menu-item>
                <el-menu-item>
                  <template #title>
                    <el-icon>
                      <Collection/>
                    </el-icon>
                    在线图书馆
                  </template>
                </el-menu-item>
              </el-sub-menu>
              <el-sub-menu index="3">
                <template #title>
                  <el-icon>
                    <Operation/>
                  </el-icon>
                  <span>个人设置</span>
                </template>
                <el-menu-item index="/index/user-setting">
                  <template #title>
                    <el-icon>
                      <User/>
                    </el-icon>
                    个人信息设置
                  </template>
                </el-menu-item>
                <el-menu-item index="/index/privacy-setting">
                  <template #title>
                    <el-icon>
                      <Lock/>
                    </el-icon>
                    账号安全设置
                  </template>
                </el-menu-item>
              </el-sub-menu>
            </el-menu>
          </el-scrollbar>
        </el-aside>
        <el-main class="main">
          <el-scrollbar style="height: calc(100vh - 55px)">
            <router-view v-slot="{Component}">
              <transition name="el-fade-in-linear" mode="out-in">
                <component :is="Component" style="height: 100%"/>
              </transition>
            </router-view>
          </el-scrollbar>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script setup>
import {get, logout} from '@/net'
import router from "@/router/main.js";
import {useStore} from "vuex";
import {computed, onMounted, reactive, ref} from "vue";
import {
  Back,
  Bell,
  ChatDotSquare, Collection,
  Document, Files,
  Location, Lock, Message, Monitor,
  Notification, Operation,
  Position,
  School, Search,
  Umbrella, User
} from "@element-plus/icons-vue";

const store = useStore();
const loading = ref(true)
const searchInput = reactive({
  type: "1",
  text: ""
});

const user = computed(() => store.state.user);
const notification = ref([])
function getNotification(id) {
  get(`api/notification/getList?uid=${id}`, data => {
    notification.value = data;
  })
}
function deleteAllNotification(){
  get(`api/notification/deleteall?uid=${store.state.user.id}`, () =>{
    getNotification(store.state.user.id)
  })
}
function commitNotification(id,url){
  get(`api/notification/deleteone?id=${id}&uid=${store.state.user.id}`, () =>{
    getNotification(store.state.user.id)
    window.open(url)
  })
}

onMounted(() => {
  console.log("在mounted");

  store.dispatch('fetchUser')
    .then(() => {
      loading.value = false;
      if (store.state.user && store.state.user.id) {
        console.log(store.state.user.id);
        getNotification(store.state.user.id);
      } else {
        console.error("User ID is not available in the store.");
      }
    })
    .catch(error => {
      console.error("Failed to fetch user:", error);
      loading.value = false; // 确保在错误情况下也关闭加载状态
    });
});


function userLogout() {
  logout(() => router.push("/"))
}
</script>

<style lang="less" scoped>
.notification-item{
  transition: all 0.3s;
  &:hover{
    color: #acbbd9;
   transform: scale(1.1);
    cursor: pointer;
  }
}
.notification{
  font-size: 22px;
  line-height: 14px;
  transition: color 0.3s;
  text-align: center;
  &:hover{
    color: grey;
    cursor: pointer;
  }
}
.main {
  height: calc(100vh - 55px);
  padding: 0;
  background-color: #f7f8fa;
}

.main-content {
  width: 100vw;
  height: 100vh;
}

.main-content-header {
  border-bottom: solid 1px var(--el-border-color);
  height: 55px;
  display: flex;
  align-items: center;
  box-sizing: border-box;

  .logo {
    height: 32px;
  }

  .user-info {
    display: flex;
    justify-content: flex-end;
    align-items: center;

    .el-avatar:hover {
      cursor: pointer;
    }

    .profile {
      text-align: right;
      margin-right: 20px;

      :first-child {
        font-size: 18px;
        font-weight: bold;
        line-height: 20px;
      }

      :last-child {
        font-size: 14px;
        color: gray;
      }
    }


  }
}

</style>